package com.macro.mall.controller;

import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.model.CurCourseType;
import com.macro.mall.service.CurCourseTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课程类型表(cur_course_type）
 *
 * @author gaodx
 * @create 2020-08-11 15:43
 **/
@Slf4j
@RestController
@RequestMapping("curCourseType")
@Api(tags = "CurCourseTypeController", description = "业务范围管理(cur_course_type）")
public class CurCourseTypeController {

    @Autowired
    CurCourseTypeService curCourseTypeService;

    @ApiOperation("查询业务范围")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<CurCourseType>> list(@RequestParam(value = "parentId", defaultValue = "0" , required = false) String parentId,
                                                        @ApiParam(value = "1-生活服务 2-社区教育" ) @RequestParam(value = "busiCategory", required = false) String busiCategory,
                                                        @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                        @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {


        List<CurCourseType> curCourseTypes = curCourseTypeService.selectAllByCurCourseType(parentId, busiCategory, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(curCourseTypes));
    }

    @ApiOperation("添加业务范围")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult save(@RequestBody CurCourseType request) {
        try {
            curCourseTypeService.saveCurCourseTypeSelective(request);
            return CommonResult.success(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.failed();
    }


    @ApiOperation("删除")
    @RequestMapping(value = "/del", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult del(@RequestParam(value = "id", required = false) String id) {
        try {
            curCourseTypeService.deleteCurCourseTypeById(id);
            return CommonResult.success(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.failed();
    }


    @ApiOperation("查询业务范围")
    @RequestMapping(value = "/getById", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult getById(@RequestParam(value = "id", required = false) String id) {
        try {
            CurCourseType curCourseType = curCourseTypeService.selectCurCourseTypeById(id);
            return CommonResult.success(curCourseType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.failed();
    }

    @ApiOperation("修改业务范围")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@RequestBody CurCourseType request) {
        try {
            curCourseTypeService.updateCurCourseTypeSelective(request);
            return CommonResult.success(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.failed();
    }

    @ApiOperation("通过机构id查询业务范围")
    @RequestMapping(value = "/listByDeptId", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<CurCourseType>> listByDeptId(@RequestParam(value = "deptId", defaultValue = "0" , required = false) String deptId,
                                                        @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                        @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {


        List<CurCourseType> curCourseTypes = curCourseTypeService.selectAllByDeptId(deptId, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(curCourseTypes));
    }



}